#!/usr/bin/env python
import urllib2,re,random

daft = None

def getip():
	f=urllib2.urlopen("http://checkip.dyndns.org/").read()
	f=re.sub("[^0-9\.]+","",f)
	return f

def getkeywords(url):
	commonwords = re.split("[^A-Za-z0-9\_\-\\\']+","the	or	will	number of	one	up	no and	had other	way a	by	about	could to	word	out	people in	but	many	my is	not	then	than you	what	them	first that	all	these	water it	were	so	been he	we	some	call was	when	her	who for	your	would	oil on	can	make	its are	said	like	now as	there	him	find with	use	into	long his	an	time	down they	each	has	day I	which	look	did at	she	two	get be	do	more	come this	how	write	made have	their	go	may from	if	see	part comments sharethis i'm read login register its nbsp http settimeout redirect_url var path cdata settabindex onhashchange tabindex")
	r = urllib2.Request(url)
	r.add_header('User-Agent','Mozilla/5.0 (X11; U; Linux x86_64; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Ubuntu/10.04 Chromium/8.0.552.224 Chrome/8.0.552.224 Safari/534.10')
	f = urllib2.urlopen(r).read()
	keywords = re.search( "<meta name=\"[Kk]eywords\".*?content=\"(.*?)\"", f )
	if keywords:
		return keywords.group(1).replace(", ",",").split(',')
	else:
		rawtext = re.sub("\<.*?\>"," ",f)
		wordlist = re.split("[^A-Za-z0-9\_\-\\\']+",rawtext)
		keywordsrank = {}
		for word in wordlist:
			word = word.lower()
			if word not in commonwords:
				if not keywordsrank.has_key(word):
					keywordsrank[word] = 0
				keywordsrank[word] += len(word)
		return sorted(keywordsrank, key=keywordsrank.__getitem__, reverse=True)

def bestkeywords(url):
	keywords = getkeywords(url)
	if len(keywords) > 25:
		keywords = keywords[:25]
	return keywords

def check_site(url):
	daft.modules['neural'].addmeme(url,"webpage")
	keywords = bestkeywords(url)
	for keyword in keywords:
		daft.modules['neural'].link(url,keyword,1)
	return random.choice(keywords)

def tick():
	pass
def start(d):
	global daft
	daft = d
def say(daft,r,who,w):
	pass
def stop():
	pass
